<!-- Designed and Developed by VSE Group -->
<!-- For any concerns :  gaurav.pruthi@tcs.com -->

<?php
session_start(); 
require_once('KLogger.php');
$log = new KLogger ( "log.txt" , KLogger::DEBUG ); 
?>	


<?php include('./classes/mailer.php'); ?>
<?php include('./classes/employee.php'); ?>


<?php
$username = $_SESSION['username'];
$id = $_SESSION['id'];
$role = $_SESSION['role'];
if($username==null || $username== ""|| $username == "")
{
	header('Location: http://172.26.195.159/checklist/php/logout.php');
}

//Connecting to databse...
$con = mysql_connect("localhost","root","");
mysql_select_db("checklist" , $con);
?>


<?php

//Value on approval is : "Approve" while on rejection it is "Reject"
if(isset($_REQUEST['approve']))
{
$check = "update cr_status set status=8 where id='$id' ";
$result = mysql_query($check) or die('Could not connect: ' . mysql_error());

//mail to designer on Approval of checklist..
  /*********************************Send Mail***************************************************/

$m = new mailer();

//Gathering data....
$sql = "select * from cr where id = '$id' ";
$result = mysql_query($sql);
$data_from_cr_for_mail = mysql_fetch_array($result);

//Feeding Data for CR..
$m->set_id($id);
$m->set_title($data_from_cr_for_mail['title']);
$m->set_status(10);

//Fetching designer's mailing address details....
$query = "SELECT email FROM employees WHERE username in(SELECT designer FROM cr where id = '$id')"; 
$result = mysql_query($query);
$mail_designer_data = mysql_fetch_array($result); 

$mail_designer = $mail_designer_data['email'];  

$log->logInfo('Designer email address : '.$id.'ok');
                     //got the mail of designer...
mysql_free_result($result);                                         //freeing the result

//Fetching reviewer's mailing address details..
//fetching usernames.
$query = "select checklist_reviewer,internal_idce_reviewer,external_idce_reviewer,pl,dp_prime from cr_status where id='$id'";
$result = mysql_query($query);
$reviewers_names = mysql_fetch_array($result);                       
$checklist_reviewer = $reviewers_names['checklist_reviewer'];
$internal_idce_reviewer = $reviewers_names['internal_idce_reviewer'];
$external_idce_reviewer = $reviewers_names['external_idce_reviewer'];
$dp_prime = $reviewers_names['dp_prime'];
$pl = $reviewers_names['pl'];

//fetching mail_addresses..
$query = "select email from employees where username = '$checklist_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);  
$mail_checklist_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$external_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_external_idce_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$pl' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_pl = $mail_reviewers['email'];

$query = "select email from employees where username = '$dp_prime' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_dp_prime = $mail_reviewers['email'];

$query = "select email from employees where username = '$internal_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_internal_idce_reviewer = $mail_reviewers['email'];

// '$checklist_reviewer' , '$internal_idce_reviewer','$external_idce_reviewer','$pl','$dp_prime'

$cc = array();
$cc[0]=$mail_checklist_reviewer;
$cc[1]=$mail_internal_idce_reviewer;
$cc[2]=$mail_external_idce_reviewer ;
$cc[3]=$mail_pl;
$cc[4]=$mail_dp_prime;


$log->logInfo('$mail_designer  : '.$mail_designer);
$log->logInfo('$mail_checklist_reviewer :'.$mail_checklist_reviewer);

//Setting to and ccs'
$m->set_to($mail_designer);

//Setting message and body..
$m->set_headers('From: "WebChecklist Notification" <WebChecklist_Message_Broker@tcs.com>'."\r\n".
"CC : ".$cc[0]." , ".$cc[1]." , ".$cc[2]." , ".$cc[3]." , ".$cc[4]);


$m->set_subject("Status of CR : ".$id." has been changed to ".getStatus(8)."");
$m->set_body("Hi , "."\n\n"."The Pre Submission for your CR : ".$id." has been approved ."."Please fill your PostSubmission form and get it approved."."\n\n\n"."Regards ,"."\n"."iVSE - WebChecklist Messenger"
."\n\n"."In case of concerns :"."\n"."mailto:gaurav.pruthi@tcs.com"."\n"."ESN : 3048");


//Sending mail...
$m->sendmail();	
//*******************************End Mail Part**********************************************


mysql_close($con);

header('Location: http://172.26.195.159/checklist/php/mainpage.php');
}  //End if condition on Approval

if(isset($_REQUEST['reject']))
{
$rejection_reason = $_REQUEST['preSubmissionReject'];
$log->logInfo('processChecklist2[45] , reason of Reject : '.$rejection_reason);

//Update new CR Status..
$check1 = "update cr_status set status=6 where id='$id' ";
$result = mysql_query($check1) or die('Could not connect: ' . mysql_error());

//Update reject Status..
$check2 = "insert into reject_status values('$id' , 'presubmission' ,'$rejection_reason')";
$result = mysql_query($check2) or die('Could not connect: ' . mysql_error());

//Delete data from checklist
$check3 = "delete from presubmission where id = '$id'";
$result = mysql_query($check3) or die('Could not connect: ' . mysql_error());

//mail to designer on rejection of checklist..
  /*********************************Send Mail***************************************************/

$m = new mailer();

//Gathering data....
$sql = "select * from cr where id = '$id' ";
$result = mysql_query($sql);
$data_from_cr_for_mail = mysql_fetch_array($result);

//Feeding Data for CR..
$m->set_id($id);
$m->set_title($data_from_cr_for_mail['title']);
$m->set_status(8);

//Fetching designer's mailing address details....
$query = "SELECT email FROM employees WHERE username in(SELECT designer FROM cr where id = '$id')"; 
$result = mysql_query($query);
$mail_designer_data = mysql_fetch_array($result); 

$mail_designer = $mail_designer_data['email'];  

$log->logInfo('Designer email address : '.$id.'ok');
                     //got the mail of designer...
mysql_free_result($result);                                         //freeing the result

//Fetching reviewer's mailing address details..
//fetching usernames.
$query = "select checklist_reviewer,internal_idce_reviewer,external_idce_reviewer,pl,dp_prime from cr_status where id='$id'";
$result = mysql_query($query);
$reviewers_names = mysql_fetch_array($result);                       
$checklist_reviewer = $reviewers_names['checklist_reviewer'];
$internal_idce_reviewer = $reviewers_names['internal_idce_reviewer'];
$external_idce_reviewer = $reviewers_names['external_idce_reviewer'];
$dp_prime = $reviewers_names['dp_prime'];
$pl = $reviewers_names['pl'];

//fetching mail_addresses..
$query = "select email from employees where username = '$checklist_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);  
$mail_checklist_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$external_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_external_idce_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$pl' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_pl = $mail_reviewers['email'];

$query = "select email from employees where username = '$dp_prime' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_dp_prime = $mail_reviewers['email'];

$query = "select email from employees where username = '$internal_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_internal_idce_reviewer = $mail_reviewers['email'];

// '$checklist_reviewer' , '$internal_idce_reviewer','$external_idce_reviewer','$pl','$dp_prime'

$cc = array();
$cc[0]=$mail_checklist_reviewer;
$cc[1]=$mail_internal_idce_reviewer;
$cc[2]=$mail_external_idce_reviewer ;
$cc[3]=$mail_pl;
$cc[4]=$mail_dp_prime;


$log->logInfo('$mail_designer  : '.$mail_designer);
$log->logInfo('$mail_checklist_reviewer :'.$mail_checklist_reviewer);

//Setting to and ccs'
$m->set_to($mail_designer);

//Setting message and body..
$m->set_headers('From: "WebChecklist Notification" <WebChecklist_Message_Broker@tcs.com>'."\r\n".
"CC : ".$cc[0]." , ".$cc[1]." , ".$cc[2]." , ".$cc[3]." , ".$cc[4]);


$m->set_subject("Status of CR : ".$id." has been changed to ".getStatus(6)."");
$m->set_body("Hi , "."\n\n"."The PreSubmission review for your CR : ".$id." has been rejected ."."\n"."Rejection reason :".$rejection_reason."\n"."Please refill it and get it approved."."\n\n\n"."Regards ,"."\n"."iVSE - WebChecklist Messenger"
."\n\n"."In case of concerns :"."\n"."mailto:gaurav.pruthi@tcs.com"."\n"."ESN : 3048");


//Sending mail...
$m->sendmail();	
//*******************************End Mail Part**********************************************
mysql_close($con);
}

header('Location: http://172.26.195.159/checklist/php/mainpage.php');



?>